package com.work.saturn.mine;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.immo.libcommon.base.BaseBean;
import com.immo.libcommon.base.BaseHeadActivity;
import com.immo.libcommon.constant.BaseUrl;
import com.immo.libcommon.constant.RouterUtil;
import com.immo.libcommon.http.NetworkRequest;
import com.work.saturn.R;
import com.work.saturn.bean.TokenMsgBean;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author Administrator
 * @content 修改Token
 * @date 2018/8/10
 */
@Route(path = RouterUtil.CHANGE_TOKEN_PAGE)
public class ChangeGasActivity extends BaseHeadActivity {
    int type;
    @BindView(R.id.token_gas_01)
    EditText mTokenGas01;
    @BindView(R.id.token_gas_02)
    EditText mTokenGas02;
    @BindView(R.id.token_Ram_01)
    EditText mTokenRam01;
    @BindView(R.id.token_Ram_02)
    EditText mTokenRam02;
    @BindView(R.id.token_name)
    TextView mTokenName;
    @BindView(R.id.token_symbol)
    TextView mTokenSymbol;
    @BindView(R.id.token_decimol)
    TextView mTokenDecimol;
    @BindView(R.id.token_supply)
    TextView mTokenSupply;
    @BindView(R.id.token_main_Account)
    TextView mTokenMainAccount;
    @BindView(R.id.token_100_Account)
    TextView mToken100Account;
    @BindView(R.id.token_101_Account)
    TextView mToken101Account;
    @BindView(R.id.token_introduction)
    TextView mTokenIntroduction;
    @BindView(R.id.token_submit)
    TextView mTokenSubmit;
    @BindView(R.id.ram_ll)
    LinearLayout ramLl;
    @BindView(R.id.ram_ll_view)
    View ramLlView;
    @BindView(R.id.ram_acc_ll)
    LinearLayout ramAccLl;
    @BindView(R.id.ram_acc_view)
    View ramAccView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.change_gas_activity);
        ButterKnife.bind(this);
        initUI();

    }

    private void initUI() {
        type = getIntent().getIntExtra("type", 0);
        if (type == 0) {
            setTitle(getIntent().getStringExtra("tokenName") + "——Modify  GAS");
            mTokenRam01.setEnabled(false);
            mTokenRam02.setEnabled(false);
            mTokenGas01.setEnabled(false);
            mTokenGas02.setEnabled(false);
            mTokenRam01.setBackgroundResource(R.color.colorWhite);
            mTokenRam02.setBackgroundResource(R.color.colorWhite);

        } else {
            setTitle("修改RAM");
            mTokenGas01.setEnabled(false);
            mTokenGas02.setEnabled(false);
            mTokenGas01.setBackgroundResource(R.color.colorWhite);
            mTokenGas02.setBackgroundResource(R.color.colorWhite);
        }
        getTokenMsg();
    }

    @SuppressLint("SetTextI18n")
    private void getTokenMsg() {
        new NetworkRequest((object, String) -> {
            if (object instanceof TokenMsgBean) {
                TokenMsgBean tokenMsgBean = (TokenMsgBean) object;
                mTokenName.setText(tokenMsgBean.getPayload().getPayload().getName());
                mTokenSymbol.setText(tokenMsgBean.getPayload().getPayload().getSymbol());
                mTokenDecimol.setText(tokenMsgBean.getPayload().getPayload().getDecimals());
                mTokenSupply.setText(tokenMsgBean.getPayload().getPayload().getAmount());
                mTokenGas01.setText("" + tokenMsgBean.getPayload().getPayload().getGasMin());
                mTokenGas02.setText("" + tokenMsgBean.getPayload().getPayload().getGasPercentage());
                if (!tokenMsgBean.getPayload().getPayload().getRamAccountId().isEmpty()) {
                    mTokenRam01.setText("" + tokenMsgBean.getPayload().getPayload().getRamMin());
                    mTokenRam02.setText("" + tokenMsgBean.getPayload().getPayload().getRamPercentage());
                    mToken101Account.setText("" + tokenMsgBean.getPayload().getPayload().getRamAccountId());
                    ramLl.setVisibility(View.VISIBLE);
                    ramLlView.setVisibility(View.VISIBLE);
                    ramAccLl.setVisibility(View.VISIBLE);
                    ramAccView.setVisibility(View.VISIBLE);
                } else {
                    ramLl.setVisibility(View.GONE);
                    ramLlView.setVisibility(View.GONE);
                    ramAccLl.setVisibility(View.GONE);
                    ramAccView.setVisibility(View.GONE);
                }


                mTokenMainAccount.setText(tokenMsgBean.getPayload().getPayload().getMintageAccountId());
                mToken100Account.setText("" + tokenMsgBean.getPayload().getPayload().getGasAccountId());
                mTokenIntroduction.setText(tokenMsgBean.getPayload().getPayload().getDescription());
            }
        }).requestGet(BaseUrl.FIND_MSG + getIntent().getStringExtra("tokenName"), ChangeGasActivity.this, TokenMsgBean.class, true, false);
    }

    @OnClick(R.id.token_submit)
    public void onViewClicked() {
        if (mTokenGas01.getText().toString().length() == 0) {
            toast("信息填写不完整");
            return;
        }
        if (mTokenGas02.getText().toString().length() == 0) {
            toast("信息填写不完整");
            return;
        }
        HashMap<String, String> params = new HashMap<>();
        params.put("gasMin", mTokenGas01.getText().toString());
        params.put("gasPercentage", mTokenGas02.getText().toString());
        new NetworkRequest((Object object, String String) -> {
            if (object instanceof BaseBean) {
                BaseBean baseBean = (BaseBean) object;
                if (baseBean.isSuccess()) {
                    toast("修改成功");
                    finish();
                }

            }
        }).requestPostKey(BaseUrl.CHANGE_MSG + getIntent().getStringExtra("tokenName"), ChangeGasActivity.this, params, BaseBean.class, true, false);
    }
}
